package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/*读事务的注解   @Inherited 这个元注解用来表示该注解可以被继承 */
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
//@Service  这个注解不能打在这儿
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T queryById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> queryAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        /*查询总条数*/
        Integer total = baseMapper.queryCount(baseQuery);
        if (total == 0){//如果总条数已经是0条 就不需要后面的查询
//            ctrl + alt + 向右或向左 反复横跳
            return new PageList<>();
        }else {
            /*查询当前页数据*/
            List<T> list = baseMapper.queryData(baseQuery);
            return new PageList<>(total,list);
        }
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        baseMapper.bulkRemove(ids);
    }
}
